package org.coderjonny.controller;

import org.coderjonny.bean.Product;
import org.coderjonny.dto.product.ProductId;
import org.coderjonny.dto.Request;
import org.coderjonny.dto.Result;
import org.coderjonny.dto.product.UpdateStock;
import org.coderjonny.service.ProductService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Validated
public class ProductController {

    @Resource
    ProductService productService;

    @PostMapping("/getByProductId")
    public Result<Product> getProductById(@Validated @RequestBody Request<ProductId> request){
        Product product = productService.getProductById(request.getData());
        return Result.ok(product);
    }
    @PostMapping("/updateStock")
    public Result<String> updateStockById(@Validated @RequestBody Request<UpdateStock> request){
        int rows = productService.updateStockById(request.getData());
        if (rows > 0){
            return Result.ok("成功");
        }else{
            return Result.error("库存不足");
        }
    }
}
